package cn.edu.neusoft.demo.hospital2.Service.impl;


import cn.edu.neusoft.demo.hospital2.Dao.DoctorDao;
import cn.edu.neusoft.demo.hospital2.Dao.DoctorWorkDao;
import cn.edu.neusoft.demo.hospital2.Dao.RoomDao;
import cn.edu.neusoft.demo.hospital2.Dao.WorkPlanDao;
import cn.edu.neusoft.demo.hospital2.Service.WorkPlanService;
import cn.edu.neusoft.demo.hospital2.pojo.Doctor;
import cn.edu.neusoft.demo.hospital2.pojo.FullDoctorWork;
import cn.edu.neusoft.demo.hospital2.pojo.Room;
import cn.edu.neusoft.demo.hospital2.pojo.WorkPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WorkPlanServiceImpl implements WorkPlanService {

    @Autowired
    private WorkPlanDao workPlanDao;
    @Autowired
    private DoctorWorkDao doctorWorkDao;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private RoomDao roomDao;

    public boolean generatePlan(WorkPlan wp) {
        int result=0;
        for(int i=1;i<=7;i++)
        {
            wp.setWeekday(i+"");
            result+=workPlanDao.addWorkPlan(wp);
            String date=wp.getDay();
            String[] dates=date.split("-");

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date(Integer.parseInt(dates[0])-1900,Integer.parseInt(dates[1])-1,Integer.parseInt(dates[2])));
            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
            String date0=1900+calendar.getTime().getYear()+"";
            String date1=1+calendar.getTime().getMonth()+"";
            String date2=calendar.getTime().getDate()+"";
            if(calendar.getTime().getMonth()<9)
                date1="0"+date1;
            if(calendar.getTime().getDate()<10)
                date2="0"+date2;
            wp.setDay(date0+"-"+date1+"-"+date2);


        }
        return result==7;
    }

    String formateDateString(Date d)
    {
        int year=1900+d.getYear();
        int month=1+d.getMonth();
        int date=d.getDate();
        String str_month=month+"";
        String str_date=date+"";
        if(month<10)
            str_month="0"+str_month;
        if(date<10)
            str_date="0"+str_date;
        String result=year+"-"+str_month+"-"+str_date;
        return result;
    }

    public int updateByDay(WorkPlan wp) {
        Date d=new Date(wp.getDay());
        int res=workPlanDao.updateByDay(this.formateDateString(d), wp.getStatus());
        return res;
    }

    public WorkPlan getPlansStatus(Date d) {
        WorkPlan wp=new WorkPlan();
        for(int i=1;i<=7;i++)
        {
            int year=1900+d.getYear();
            int month=1+d.getMonth();
            int date=d.getDate();
            int day=d.getDay();
            WorkPlan w=workPlanDao.selectByDay(year,month,date);
            System.out.println(w);
            if(w==null) {
                return null;
            }
            switch(day)
            {
                case 1://星期一
                    wp.setMondayDay(d);
                    wp.setMondayStatus(w.getStatus());
                    break;
                case 2://星期二
                    wp.setTuesdayDay(d);
                    wp.setTuesdayStatus(w.getStatus());
                    break;
                case 3://星期三
                    wp.setWednesdayDay(d);
                    wp.setWednesdayStatus(w.getStatus());
                    break;
                case 4://星期四
                    wp.setThursdayDay(d);
                    wp.setThursdayStatus(w.getStatus());
                    break;
                case 5://星期五
                    wp.setFridayDay(d);
                    wp.setFridayStatus(w.getStatus());
                    break;
                case 6://星期六
                    wp.setSaturdayDay(d);
                    wp.setSaturdayStatus(w.getStatus());
                    break;
                case 7://星期日
                    wp.setSundayDay(d);
                    wp.setSundayStatus(w.getStatus());
                    break;
            }
            Calendar   calendar = new GregorianCalendar();
            calendar.setTime(d);
            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
            d=calendar.getTime(); //这个时间就是日期往后推一天的结果

        }
        return wp;

    }

    /*
     * 获得指定日期的医生排班情况
     * */
    public List<FullDoctorWork> getAllBaseInfo(String day) {
        List<FullDoctorWork> dws=this.doctorWorkDao.selectAllInfo(day);
        List<Doctor> doctors=this.doctorDao.selectList(null);
        List<Room> rooms=this.roomDao.selectList(null);
        String[] dwIds=new String[dws.size()];
        for(int i=0;i<dws.size();i++)
            dwIds[i]=dws.get(i).getDocId();
        if(dws.size()<doctors.size())//处理尚未排班的医生
        {
            for(Doctor d:doctors)
            {
                if(!Arrays.asList(dwIds).contains(d.getDocId()))
                {
                    FullDoctorWork fdw=new FullDoctorWork();
                    fdw.setDocId(d.getDocId());
                    fdw.setDocName(d.getDocName());
                    fdw.setDocTitle(d.getDocTitle());
                    fdw.setDepartId1(d.getDepartId1());
                    fdw.setDepartId2(d.getDepartId2());

                    dws.add(fdw);
                }
            }
        }
        for(FullDoctorWork fdw:dws)
        {
            List<Room> myRoom=new ArrayList<Room>();
            for(Room room:rooms)
            {
                if(fdw.getDepartId1()==room.getDepartId1())
                {
                    myRoom.add(room);
                }
            }
            fdw.setRooms(myRoom);
        }
        return dws;
    }
    @Override
    public List<WorkPlan> getAllWorkPlans() {
        return workPlanDao.getAllWorkPlans();
    }

    @Override
    public WorkPlan getWorkPlanByDay(java.sql.Date day) {
        return workPlanDao.getWorkPlanByDay(day);
    }

    @Override
    public boolean addWorkPlan(WorkPlan workPlan) {
        return workPlanDao.addWorkPlan(workPlan) > 0;
    }

    @Override
    public boolean updateWorkPlan(WorkPlan workPlan) {
        return workPlanDao.updateWorkPlan(workPlan) > 0;
    }

    @Override
    public boolean deleteWorkPlan(java.sql.Date day) {
        return workPlanDao.deleteWorkPlan(day) > 0;
    }
}
